package com.st.springts.controller;


import com.st.springts.demos.web.Page;
import com.st.springts.demos.web.Result;
import com.st.springts.entity.User;
import com.st.springts.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /*

     */
    @PostMapping("/add")
    public Result add(@RequestBody User user) {


        try {
            userService.insertUser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error(500, "数据库错误");
            } else {
                return Result.error(500, "系统错误");
            }
        }
        return Result.success("sucess", user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {

        userService.updateUser(user);
        return Result.success("sucess", user);
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteUser(id);
     

        return Result.success("SUCESS", id);
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        userService.batchDeleteUser(ids);

        return Result.success("SUCESS", ids);
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> users = userService.selectAll();

        return Result.success("sucess", users);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user = userService.selectById(id);

        return Result.success("sucess", user);
    }

    @GetMapping("/selectByMore")
    public Result selectByMore(@RequestParam String username, @RequestParam String name) {
        List<User> users = userService.selectByMore(username, name);

        return Result.success("sucess", users);
    }

    @GetMapping("/selectByMo")
    public Result selectByMo(@RequestParam String username, @RequestParam String name) {
        List<User> users = userService.selectByMo(username, name);

        return Result.success("sucess", users);
    }

    @GetMapping("/selectByPage")
    public Result selectByPage(
            @RequestParam Integer pageNum,@RequestParam Integer pageSize,
            @RequestParam String username, @RequestParam String name) {

        Page<User> page = userService.selectByPage(pageNum,pageSize,username, name);

        return Result.success("sucess", page);
    }

    @GetMapping("/selectByUsername")
    public Result selectByUsername(@RequestParam String username){
        Integer id=  userService.selectByUsername(username);
        return Result.success("sucess",id);
    }

    @GetMapping("/ab")
    public Result selectab(@RequestParam Integer id){
             String c=    userService.selectab(id);
        System.out.println(c);
                
        return Result.success("sucess",c);
    }

    @GetMapping("/aa")
    public void selectaa(){
    userService.selectaa();

    }

}
